using System.Collections.ObjectModel;
using System.Linq;
using RapidDownloader.Model;
using Vidyano;
using Vidyano.Module;

namespace RapidDownloader.Modules.DownloadModule
{
    sealed partial class DownloadModule : ModuleViewModel<DownloadContext>
    {
        #region Private Property Backers

        private bool _CanMoveUpInQueue, _CanMoveDownInQueue;
        private double _DownloadProgress;

        #endregion

        #region Constructor

        public DownloadModule()
        {
            Subscriptions.Subscribe(MessageBus.Listen<FileDownloadProgressMessage>().Where(fpm => Context.Files.Contains(fpm.File)), OnFileDownloadProgressChanged);
        }

        #endregion

        #region Properties

        public static ObservableCollection<DownloadModule> Downloads
        {
            get { return DownloadQueueModule.DownloadQueueModule.Current.QueuedDownloads; }
        }

        public override string TitleWithSuffix
        {
            get
            {
                return Title;
            }
        }

        public bool CanMoveUpInQueue
        {
            get
            {
                return _CanMoveUpInQueue;
            }
            private set
            {
                _CanMoveUpInQueue = value;
                SendPropertyChanged("CanMoveUpInQueue");
            }
        }

        public bool CanMoveDownInQueue
        {
            get
            {
                return _CanMoveDownInQueue;
            }
            private set
            {
                _CanMoveDownInQueue = value;
                SendPropertyChanged("CanMoveDownInQueue");
            }
        }

        public double DownloadProgress
        {
            get
            {
                return _DownloadProgress;
            }
            private set
            {
                _DownloadProgress = value;
                SendPropertyChanged("DownloadProgress");
            }
        }

        #endregion

        #region Methods

        public override void GoBack()
        {
            // Don't allow to go back from the downloadpage to the links page
        }

        public void MoveUpInQueue()
        {
            var index = Downloads.IndexOf(this);
            if (index > 0)
                Downloads.Move(index, index - 1);
        }

        public void MoveDownInQueue()
        {
            var index = Downloads.IndexOf(this);
            if (index < Downloads.Count - 1)
                Downloads.Move(index, index + 1);
        }

        internal void InvalidateCanMoveInQueue()
        {
            var index = Downloads.IndexOf(this);
            CanMoveUpInQueue = index > 0;
            CanMoveDownInQueue = index < Downloads.Count - 1;
        }

        protected override void OnClose()
        {
            Downloads.Remove(this);
            base.OnClose();
        }

        private void OnFileDownloadProgressChanged(FileDownloadProgressMessage e)
        {
            var files = Context.Files;
            DownloadProgress = (100.0 / files.Sum(f => f.Size)) * files.Sum(f => f.TotalBytes);
        }

        #endregion
    }
}